package com.noble.meether.db;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;

import com.noble.dbcenter.DBManager;
import com.noble.glamour.module.logic.application.App;
import com.noble.library.log.PLogger;
import com.noble.library.observe.ModuleBase;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.meether.chat.ui.system.store.DBNoticeCache;
import com.noble.meether.db.init.DBHelper;
import com.noble.meether.db.type.msg.DBMessageCache;
import com.noble.meether.fragment.mail.store.DBMessageListCache;
import com.squareup.sqlbrite.BriteDatabase;
import com.squareup.sqlbrite.BuildConfig;
import com.squareup.sqlbrite.SqlBrite;

import rx.schedulers.Schedulers;

/**
 * 数据库操作管理类
 */
public class DBMgr implements ModuleBase, PObserver {

    @Override
    public void init() {
        MsgMgr.getInstance().attach(this);
    }

    @Override
    public void release() {
        MsgMgr.getInstance().detach(this);
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_App_Login:
                if ((Boolean) value) {
                    initDB();
                }
                break;
        }
    }

    /**
     * 数据表： 消息
     */
    private DBMessageCache msgCache;

    private void initMessageCache() {
        msgCache = new DBMessageCache(mDatabase, handler);
    }

    public DBMessageCache getMsgCache() {
        return msgCache;
    }

    /**
     * 聊天用户列表
     */
    private DBMessageListCache msgListCache;

    private void initMessageListCache() {
        msgListCache = new DBMessageListCache(mDatabase, handler);
    }

    public DBMessageListCache getMsgListCache() {
        return msgListCache;
    }

    /**
     * 系统公告表
     */
    private DBNoticeCache noticeCache;

    private void initNoticeCache() {
        noticeCache = new DBNoticeCache(mDatabase, handler);
    }

    public DBNoticeCache getNoticeCache() {
        return noticeCache;
    }

    // -------------------------------------------- 数据库注册 ----------------------------------------
    private BriteDatabase mDatabase;

    // 工作线程
    private HandlerThread workerThread = new HandlerThread("LightTaskThread");
    private DBHandler handler = null;

    /**
     * DB初始化
     */
    private void initDB() {
        if (App.uid > 0) {
            DBHelper dbHelper = new DBHelper(App.getContext(), String.valueOf(App.uid));
            SqlBrite sqlBrite = new SqlBrite.Builder().build();
            mDatabase = sqlBrite.wrapDatabaseHelper(dbHelper, Schedulers.io());
            mDatabase.setLoggingEnabled(BuildConfig.DEBUG);
            DBManager.initialize(dbHelper);

            initHandler();
            initMessageCache();     // 消息表
            initMessageListCache(); // 聊天用户列表
            initNoticeCache();      // 系统公告表
        }
    }

    private void initHandler() {
        if (!workerThread.isAlive())
            workerThread.start();
        handler = new DBHandler(workerThread.getLooper());
    }

    public class DBHandler extends Handler {
        private DBHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void dispatchMessage(Message msg) {
            try {
                super.dispatchMessage(msg);
            } catch (Exception e) {
                PLogger.e("db dispatchMessage " + e.getMessage());
            }
        }
    }
}
